package com.chentf.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author chentengfei
 * @desc ...
 * @date 2021-08-31 22:08:56
 */
@RestController
public class DemoController {

    private static Logger log = LoggerFactory.getLogger(DemoController.class);

    /***
        value:在进行流控时,资源名称不能配置为访问路径,必须配置访问路径的资源别名
        blockHandler:使用sentinel进行不同规则控制时的默认处理方案,根据抛出的不同异常进行控制.
        fallback:自定义业务出错时默认处理方案
        defaultFallback:指定一个业务错误时默认方案
     */
    @GetMapping("/demo")
    @SentinelResource(value = "chentf",blockHandler = "blockHandler",fallback = "fall",defaultFallback = "defaultFall")
    public String demo(Integer id) {
        log.info("demo ok ....");
        if(id < 0){
            throw new RuntimeException("id无效");
        }
        return "demo ok !!!";
    }

    public String blockHandler(Integer id, BlockException e){
        if(e instanceof FlowException){
            return "当前请求过于火爆,您已被流控!!!";
        }
        if(e instanceof DegradeException){
            return  "当前请求过于火爆,您已被降级!!!";
        }
        if(e instanceof ParamFlowException){
            return  "当前请求过于火爆,您已被热点参数限流!!!";
        }
        return "服务器快爆了,请稍后再试!!!";
    }

    public String fall(Integer id){
        return "自定义服务器出错啦!!!";
    }

    public String defaultFall(){
        return "默认服务器出错啦!!!";
    }

}
